package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.vo.SysStoreRequestVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysStoreVo;
import org.dromara.system.domain.bo.SysStoreBo;
import org.dromara.system.service.ISysStoreService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 系统管理-门店配置
 * 前端访问路由地址为:/system/store
 *
 * @author LionLi
 * @date 2024-03-18
 */
@Validated
@RequiredArgsConstructor
@RestController
    @RequestMapping("/store")
public class SysStoreController extends BaseController {

    private final ISysStoreService sysStoreService;

    /**
     * 查询系统管理-门店配置列表
     */

    @GetMapping("/list")
//    @SaCheckPermission("system:store:list")
    public TableDataInfo<SysStoreVo> list(SysStoreBo bo, PageQuery pageQuery) {
        return sysStoreService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出系统管理-门店配置列表
     */
    @SaCheckPermission("system:store:export")
    @Log(title = "系统管理-门店配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysStoreBo bo, HttpServletResponse response) {
        List<SysStoreVo> list = sysStoreService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统管理-门店配置", SysStoreVo.class, response);
    }

    /**
     * 获取系统管理-门店配置详细信息
     *
     * @param storeId 主键
     */
//    @SaCheckPermission("system:store:query")
    @GetMapping("/{storeId}")
    public R<SysStoreVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long storeId) {
        return R.ok(sysStoreService.queryById(storeId));
    }

    /**
     * 新增系统管理-门店配置
     */
    @SaCheckPermission("system:store:add")
    @Log(title = "系统管理-门店配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody SysStoreBo bo) {
        if (!sysStoreService.checkStoreCode(bo)) {
            return R.fail("新增参数'" + bo.getStoreCode()+ "'失败，店铺编码已存在");
        }
        return R.ok(sysStoreService.insertByBo(bo));
    }

    /**
     * 修改系统管理-门店配置
     */
    @SaCheckPermission("system:store:edit")
    @Log(title = "系统管理-门店配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysStoreBo bo) {
        if (!sysStoreService.checkStoreCode(bo)) {
            return R.fail("修改参数'" + bo.getStoreCode()+ "'失败，店铺编码已存在");
        }
        return toAjax(sysStoreService.updateByBo(bo));
    }


    /**
     * 门店配置-门店配置批量时间设置
     */
    @SaCheckPermission("system:store:edit")
    @Log(title = "系统管理-门店配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateTime")
    public R<Void> updateTime( @RequestBody SysStoreRequestVo vo) {
        return toAjax(sysStoreService.updateTime(vo));
    }

    @SaCheckPermission("system:store:remove")
    @Log(title = "系统管理-门店配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeId}")
    public R<Void> remove(@PathVariable Long storeId) {
        return toAjax(  sysStoreService.deleteStoreById(storeId));
    }
}
